package cn.iluwen.airline.Mapper;

import cn.iluwen.airline.Entities.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {

    /**
     * 查询所有订单
     * @return 订单列表
     */
    @Select("SELECT * FROM orders")
    List<Order> findAllOrders();
    
    /**
     * 分页查询订单
     * @return 订单列表
     */
    @Select("SELECT * FROM orders ORDER BY created_at DESC")
    List<Order> findOrdersWithPaging();
    
    /**
     * 根据用户ID查询订单
     * @param userId 用户ID
     * @return 订单列表
     */
    @Select("SELECT * FROM orders WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Order> findByUserId(String userId);
    
    /**
     * 根据订单号查询订单
     * @param orderNumber 订单号
     * @return 订单对象
     */
    @Select("SELECT * FROM orders WHERE order_number = #{orderNumber}")
    Order findByOrderNumber(String orderNumber);
    
    /**
     * 根据ID查询订单
     * @param orderId 订单ID
     * @return 订单对象
     */
    @Select("SELECT * FROM orders WHERE order_id = #{orderId}")
    Order findById(String orderId);
    
    /**
     * 添加订单
     * @param order 订单对象
     * @return 影响的行数
     */
    @Insert("INSERT INTO orders (order_id, user_id, order_number, total_amount, order_status, " +
            "contact_name, contact_phone, contact_email, created_at) " +
            "VALUES (#{orderId}, #{userId}, #{orderNumber}, #{totalAmount}, #{orderStatus}, " +
            "#{contactName}, #{contactPhone}, #{contactEmail}, #{createdAt})")
    int insert(Order order);
    
    /**
     * 更新订单状态
     * @param orderId 订单ID
     * @param orderStatus 订单状态
     * @return 影响的行数
     */
    @Update("UPDATE orders SET order_status = #{orderStatus} WHERE order_id = #{orderId}")
    int updateStatus(@Param("orderId") String orderId, @Param("orderStatus") String orderStatus);
    
    /**
     * 更新订单信息
     * @param order 订单对象
     * @return 影响的行数
     */
    @Update("UPDATE orders SET user_id = #{userId}, order_number = #{orderNumber}, " +
            "total_amount = #{totalAmount}, order_status = #{orderStatus}, " +
            "contact_name = #{contactName}, contact_phone = #{contactPhone}, " +
            "contact_email = #{contactEmail} WHERE order_id = #{orderId}")
    int update(Order order);
    
    /**
     * 删除订单
     * @param orderId 订单ID
     * @return 影响的行数
     */
    @Delete("DELETE FROM orders WHERE order_id = #{orderId}")
    int delete(String orderId);

    /**
     * 根据订单状态查询订单
     */
    @Select("SELECT * FROM orders WHERE order_status = #{status} ORDER BY created_at DESC")
    List<Order> findByStatus(String status);
} 